<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { ref, watch } from 'vue'
import {userLoginService, userRegisterService} from '@/api/user.js'
import { useUserStore } from '@/stores/index.js'
import { useRouter } from 'vue-router'
// 注册和登录切换使用
const isRegister = ref(false)
// 表单对象信息
const formModel = ref({
  // 用户名
  username: '',
  // 密码
  password: '',
  // 确认密码
  repassword: ''
})
// 表单校验规则
const rules = ref({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, max: 10, message: '用户名必须是5-10位的字符', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { pattern: /^\S{6,15}$/,  message: '用户名必须是5-10位的字符', trigger: 'blur' },
  ],
  repassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    { pattern: /^\S{6,15}$/,  message: '用户名必须是5-10位的字符', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        //判断两次的密码是否一致
        if (formModel.value.password !== value) {
          callback(new Error('两次密码不一致'))
        } else  {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
})
// 获取ref
const form = ref()
// 注册是否校验
const register = () => {
  if (!form.value) return
  form.value.validate(async (valid,fields)=>{
    if (valid) {
      // 调用注册接口
     const  res = await userRegisterService(formModel.value)
      console.log(res, '注册接口')
      ElMessage.success('注册成功')
      isRegister.value = false
    } else {
      console.log('error submit!', fields)
    }
  })
}
// 切换的时候, 要清空输入框
watch(isRegister,(newVal,oldVal) => {
  if (newVal !== oldVal) {
    // 清空输入框, 从新赋值一个新的对象
    formModel.value = {
      username: '',
      password: '',
      repassword: ''
    }
  }
})
// 登录
const userStore = useUserStore()
const router = useRouter()
const login = () => {
  if (!form.value) return
  form.value.validate(async (valid,fields)=>{
    if (valid) {
      // 调用登录接口
     // const { data } = await userLoginService(formModel.value)
     //  console.log(data, '登录成功')
      // 存入token, 持久化token
      // userStore.setToken(data.token || 'bear sdfsdhfjsdklfskdhfskdjfhdk')
      userStore.setToken('bear sdfsdhfjsdklfskdhfskdjfhdk')
      // const  res = await userRegisterService(formModel.value)
      ElMessage.success('登录成功')
      // 跳转首页
      router.push('/')
      isRegister.value = false
    } else {
      console.log('error submit!', fields)
    }
  })
}
</script>

<template>
  <el-row class="login-page">
    <el-col :span="12" class="bg"></el-col>
    <el-col :span="6" :offset="3" class="form">
      <el-form ref="form" size="large" autocomplete="off"
               :rules="rules" v-if="isRegister" :model="formModel">
        <el-form-item>
          <h1>注册</h1>
        </el-form-item>
        <el-form-item prop="username">
            <el-input v-model.trim="formModel.username" :prefix-icon="User" placeholder="请输入用户名" ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
            v-model.trim="formModel.password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入再次密码"
            v-model.trim="formModel.repassword"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="button" type="success" auto-insert-space @click="register">
            注册
          </el-button>
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="isRegister = false">
            ← 返回
          </el-link>
        </el-form-item>
      </el-form>
      <el-form ref="form" :rules="rules" :model="formModel" size="large" autocomplete="off" v-else>
        <el-form-item>
          <h1>登录</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input v-model.trim="formModel.username" :prefix-icon="User" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            name="password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
            v-model.trim="formModel.password"
          ></el-input>
        </el-form-item>
        <el-form-item class="flex">
          <div class="flex">
            <el-checkbox>记住我</el-checkbox>
            <el-link type="primary" :underline="false">忘记密码？</el-link>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button class="button" type="success" auto-insert-space @click="login">登录</el-button>
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="isRegister = true">
            注册 →
          </el-link>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<style lang="scss" scoped>
.login-page {
  height: 100vh;
  background-color: #fff;
  .bg {
    background: url('@/assets/login.png') no-repeat 60% center / 240px auto,
    url('@/assets/login.png') no-repeat center / cover;
    border-radius: 0 20px 20px 0;
  }
  .form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    user-select: none;
    .title {
      margin: 0 auto;
    }
    .button {
      width: 100%;
    }
    .flex {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>
